package cn.iocoder.yudao.module.call.controller.admin.ivrflowvoice;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.ivrflowvoice.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrflowvoice.IvrFlowVoiceDO;
import cn.iocoder.yudao.module.call.service.ivrflowvoice.IvrFlowVoiceService;

@Tag(name = "管理后台 - ivr流程-语音文件")
@RestController
@RequestMapping("/call/ivr-flow-voice")
@Validated
public class IvrFlowVoiceController {

    @Resource
    private IvrFlowVoiceService ivrFlowVoiceService;

    @PostMapping("/create")
    @Operation(summary = "创建ivr流程-语音文件")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createIvrFlowVoice(@Valid @RequestBody IvrFlowVoiceSaveReqVO createReqVO) {
        return success(ivrFlowVoiceService.createIvrFlowVoice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ivr流程-语音文件")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:update')")
    public CommonResult<Boolean> updateIvrFlowVoice(@Valid @RequestBody IvrFlowVoiceSaveReqVO updateReqVO) {
        ivrFlowVoiceService.updateIvrFlowVoice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ivr流程-语音文件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:delete')")
    public CommonResult<Boolean> deleteIvrFlowVoice(@RequestParam("id") Long id) {
        ivrFlowVoiceService.deleteIvrFlowVoice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ivr流程-语音文件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<IvrFlowVoiceRespVO> getIvrFlowVoice(@RequestParam("id") Long id) {
        IvrFlowVoiceDO ivrFlowVoice = ivrFlowVoiceService.getIvrFlowVoice(id);
        return success(BeanUtils.toBean(ivrFlowVoice, IvrFlowVoiceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ivr流程-语音文件分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<PageResult<IvrFlowVoiceRespVO>> getIvrFlowVoicePage(@Valid IvrFlowVoicePageReqVO pageReqVO) {
        PageResult<IvrFlowVoiceDO> pageResult = ivrFlowVoiceService.getIvrFlowVoicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrFlowVoiceRespVO.class));
    }

    @GetMapping("/getIvrFlowVoiceByType")
    @Operation(summary = "根据type获得ivr流程-语音文件")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<List<IvrFlowVoiceRespVO>> getIvrFlowVoiceByType(@Valid IvrFlowVoicePageReqVO pageReqVO) {
        List<IvrFlowVoiceDO> pageResult = ivrFlowVoiceService.getIvrFlowVoiceByType(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrFlowVoiceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ivr流程-语音文件 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrFlowVoiceExcel(@Valid IvrFlowVoicePageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrFlowVoiceDO> list = ivrFlowVoiceService.getIvrFlowVoicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ivr流程-语音文件.xls", "数据", IvrFlowVoiceRespVO.class,
                BeanUtils.toBean(list, IvrFlowVoiceRespVO.class));
    }

}